﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HCommon;

namespace eStock.hControl
{
    public partial class hForm : Form
    {
        public hForm()
        {
            InitializeComponent();
        }
        public enum Mode { Insert, Update, }

        protected Button hSaveButton;
        [Category("1-HCOM"), DefaultValue(false), Description("Nút Save")]
        public Button SaveButton
        {
            get { return hSaveButton; }
            set { hSaveButton = value; }
        }

        protected Button hQueryButton;
        [Category("1-HCOM"), DefaultValue(false), Description("Nút Query")]
        public Button QueryButton
        {
            get { return hQueryButton; }
            set { hQueryButton = value; }
        }

        protected Button hNewButton;
        [Category("1-HCOM"), DefaultValue(false), Description("Nút New")]
        public Button NewButton
        {
            get { return hNewButton; }
            set { hNewButton = value; }
        }

        protected Button hCloseButton;
        [Category("1-HCOM"), DefaultValue(false), Description("Nút Close")]
        public Button CloseButton
        {
            get { return hCloseButton; }
            set { hCloseButton = value; }
        }

        protected Mode hFormMode = Mode.Insert;
        [Category("1-HCOM"), DefaultValue(Mode.Insert), Description("Mode của form (Insert hay Update"), ReadOnly(true)]
        public Mode FormMode
        {
            get { return hFormMode; }
            set { hFormMode = value; }
        }

        protected string hUpdateKey = "";
        [Category("1-HCOM"), DefaultValue(""), Description("Key update"), ReadOnly(true)]
        public string UpdateKey
        {
            get { return hUpdateKey; }
            set { hUpdateKey = value; }
        }

        public hForm(IContainer container)
        {
            container.Add(this);
            InitializeComponent();
        }

        private void hForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Q && e.Control == true)
            {
                if (hQueryButton != null)
                {
                    hQueryButton.PerformClick();
                }
            }
            else if (e.KeyCode == Keys.S && e.Control == true)
            {
                if (hSaveButton != null)
                {
                    hSaveButton.PerformClick();
                }
            }
            else if (e.KeyCode == Keys.N && e.Control == true)
            {
                if (hNewButton != null)
                {
                    hNewButton.PerformClick();
                }
            }
            else if (e.KeyCode == Keys.Escape)
            {
                if (hCloseButton != null)
                {
                    hCloseButton.PerformClick();
                }
            }
        }

        private void hForm_Load(object sender, EventArgs e)
        {
            foreach (Control c1 in this.Controls)
            {
                if (c1.HasChildren)
                {
                    foreach (Control c2 in c1.Controls)
                    {
                        if (c2.GetType().Name.Contains("hComboBox"))
                        {
                            hComboBox hCom = (hComboBox)c2;
                            if (hCom.FBCode == null)
                            {
                                return;
                            }
                            if (hCom.FBCode.Trim().Length < 3)
                            {
                                return;
                            }

                            if (hCom.NotEmpty == true)
                            {
                                Common.LoadCombo(hCom, hCom.FBCode, false);
                            }
                            else
                            {
                                Common.LoadCombo(hCom, hCom.FBCode, true);
                            }
                        }
                    }
                }
                else
                {
                    if (c1.GetType().Name.Contains("hComboBox"))
                    {
                        hComboBox hCom = (hComboBox)c1;
                        if (hCom.FBCode == null)
                        {
                            return;
                        }
                        if (hCom.FBCode.Trim().Length < 3)
                        {
                            return;
                        }

                        if (hCom.NotEmpty == true)
                        {
                            Common.LoadCombo(hCom, hCom.FBCode, false);
                        }
                        else
                        {
                            Common.LoadCombo(hCom, hCom.FBCode, true);
                        }
                    }
                }
            }
        }

    }
}
